<?php
/* 
 *违规信息
 */ 
namespace Admin\Model;
use Think\Model; 
class FineUserModel extends Model {
  //违规信息
  public function violationsIndex($arr,$m_psize){
      $psize       = $arr['psize'];
      $p           = $arr['p'];
      if( !$psize ) 
          $psize   = $m_psize;
      else if( $psize > 20 )
          $psize   = $m_psize;
      //搜索条件 
      if(!empty($arr['customer'])){
        $customer_ = $arr['customer'];
        if(strlen($customer_) > 3 && is_numeric($customer_))  $map['customer.phone'] = array('like','%'.$customer_.'%');
        $map['customer.nickname']       = array('like','%'.$customer_.'%');
        $map['customer.customer_code']  = array('like','%'.$customer_.'%');
        $map['_logic']          = 'or';
        $where['_complex'] = $map;
      }
      if(!empty($arr['sales'])){
        $where['user.nickname'] = array('like','%'.$arr['sales'].'%');;
      }

      $stime    = I('get.first_time','','trim');
      $etime    = I('get.second_time','','trim');

      //区间与时间检索
      if($stime && $etime){
          $time1 = date("Y-m-d 00:00:00",strtotime($stime));
          $time2 = date("Y-m-d 23:59:59",strtotime($etime));
          if(strtotime($time1) < strtotime($time2)) $where['fine.pub_time'] =  array('between', array($time1, $time2 ) );
          else $where['fine.pub_time'] =  array('between', array( $time2 ,$time1) );
      }elseif($stime && !$etime){
          $time =  date("Y-m-d 00:00:00",strtotime($stime));
          $where['fine.pub_time'] = array('egt', $time );
      }elseif(!$stime && $etime){
          $time = date("Y-m-d 23:59:59",strtotime($etime));
          $where['fine.pub_time'] = array('elt', $time );
      }
     $where['fine.fine_type'] = array('eq','2');
      //首页数据
        $list = M("contract")
                ->field("fine.fine_id,fine.fine_type,fine.status,fine.theme,fine.pub_time,fine.remark as fine_remark,contract.contract_id,contract.start_time,contract.order_num as contract_order,contract.mount as contract_mount,contract.state as contract_state,contract.weigui_status,contract.product_id,contract.customer_id,contract.sales_person,fine_user.fine_mount,fine_user.sales_id,customer.customer_code,customer.remark,customer.nickname")
             ->join("LEFT JOIN customer  ON contract.customer_id = customer.customer_id")
             ->join("LEFT JOIN user      ON contract.sales_person = user.user_id")
             ->join("LEFT JOIN fine      ON contract.contract_id = fine.contract_id")
             ->join("LEFT JOIN fine_user ON fine.fine_id = fine_user.fine_id")
             ->where($where)
            ->order("fine.update_time desc")
            ->select();
      foreach ($list as &$value) {
          //产品
          $pr['product_id'] = $value['product_id'];
          $product = M("product")->field("name")->where($pr)->find();
          $value['name'] = $product['name'];
          //员工
          $sales['user_id'] = $value['sales_person'];
          $person = M("user")->field("nickname")->where($sales)->find();
          $value['user_name'] = $person['nickname'];
          //客户
          $maps['customer_id'] = $value['customer_id'];
          $customer = M("customer")->field("phone,location,state,mount,remark,nickname as customer_name")->where($maps)->find();
          $value['phone'] = $customer['phone'];
          $value['location'] = $customer['location'];
          $value['state'] = $customer['state'];
          $value['mount'] = $customer['mount'];
          $value['remark'] = $customer['remark'];
          $value['customer_name'] = $customer['customer_name'];

          $value['pub_time']   = substr($value['pub_time'], 0,10);
          $value['start_time'] = date("Y-m",strtotime($value['start_time']));
          $value['fine_mount'] = floor($value['fine_mount']);
          $value['mount']      = sprintf("%.2f", $value['mount']);
          $value['contract_mount'] = floor($value['contract_mount']);
      }
      //分页计算 
      $count = count($list);
      $Page  = new \Common\ORG\Page( $count, $psize );
      for($i =($p-1)*$psize;$i<$p*$psize;$i++){
          $calculate[$i] = $list[$i];
       }
       foreach ($calculate as $v) {
          if(!empty($v)){
              $result[] = $v;
          }
       }
       if($result){
          $res['data']['list']    = $result;
          $res['data']['page']    =  $Page->getTotalPages();
          $res['data']['p']       = $Page->nowPage;
          $res['data']['psize']   = $psize;
          $res['rcode']           = 1;
      }
     return $res;
  }
  //违规信息添加
  public function violationsAdd($arr){
    $uinfo = UserModel::loadFromSession();
    //默认字段
    $map['fine_num']    = ''; 
    $map['title']       = ''; 
    $map['description'] = ''; 
    $map['reply_action']= '';   
    $map['show_way']    = ''; 
    //添加字段
    $map['theme']       = $arr['theme'];
    $map['pub_time']    = $arr['fine_pub_time'];
    $map['contract_id'] = $arr['contract'];
    $note1              = $arr['attribution']; 
    $note2              = $arr['remark']; 
    $map['remark']      = $note1.",".$note2;
    $map['status']      = 1;
    $map['update_time'] = date("Y-m-d H:i:s"); 
    $map['creator']     = $uinfo['user_id'];
    $map['fine_type']   = 2;
    $add = M("fine")->add($map);
    //添加处罚信息
    if($add){
       $maps['fine_id']        = $add;
       $maps['fine_type']      = 1;
       $maps['fine_mount']     = $arr['fine_mount'];
       $maps['fine_remark']    = '';

        $maps['sales_id']      = $arr['person'];
        $maps['sales_sector']  = $user['sales_sector'];
        $maps['sales_manager'] = $manager['sales_manager'];
        $maps['sales_subcompany'] = $user['company'];
        $list = M("fine_user")->add($maps);
        if($list){
          $wg_['contract_id'] = $arr['contract'];
          $check_contract = M("contract")->field("weigui_status")->where($wg_)->find();
          if($check_contract['weigui_status'] == 0){
            $wg_state['weigui_status'] = 12;
            $contract = M("contract")->where($wg_)->save($wg_state);
          }else{
            $contract = 1;
          }
        }
      }
    if($contract){
      $res['rcode'] = 1;
    }else{
      if($add)      $res['msg'] = "添加失败!!!";
      if($list)     $res['msg'] = "关联信息添加失败!!!";
      if($contract) $res['msg'] = "合同状态更新失败!!!";
    }	
    return $res;
  }
  public function User(){
    $uinfo = UserModel::loadFromSession();
    return $uinfo ;
  }

    //删除
    public function ddelete( $fid , $uid){
        $rback = array('rcode'=>0, 'data'=>null, 'msg'=>'');
        if ( $fid && is_numeric($fid) && $fid > 0 && $uid && is_numeric($uid) && $uid > 0 ) {
            $where = array();
            $where['fine_id'] = array('eq', $fid);
            $where['sales_id'] = array('eq', $uid);
            if ($this->where( $where )->delete( )) {
                $rback['rcode'] = 1;
                $rback['msg'] = '删除成功';
            }else{
                $rback['msg'] = '很抱歉，删除成功';
            }
        }else{
            $rback['msg'] = '参数错误';
        }
        return $rback;  
    }
    //检查是否为最后一条记录
    public function checkOne( $fid ){
        $rback = array('rcode'=>0, 'data'=>null, 'msg'=>'');
        if ( $fid && is_numeric($fid) && $fid > 0 ) {
            $where = array();
            $where['fine_id'] = array('eq', $fid);
            $sdata = $this->where( $where )->select();
            if ( count($sdata) == 1 ) {
                $rback['rcode'] = 1;
            }
        }
        return $rback;  
    }
}